//
//  MainFirst_Magazine_TBV_WebCel_WX.m
//  ZITHER
//
//  Created by weixin on 2018/11/7.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "MainFirst_Magazine_TBV_WebCel_WX.h"
#import "MainFirst_MagazineVideo_VC_WX.h"
#import "MainFirst_Magazine_VC_WX.h"


@implementation MainFirst_Magazine_TBV_WebCel_WX

NSString * const JS_BRIDGE_NAME = @"JSBridge";
//key of the Java Script Core Context in web view
NSString * const JS_CONTEXT_PATH = @"documentView.webView.mainFrame.javaScriptContext";


- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    [self addSubview:self.webview];
    
    [self context];
    [SVProgressHUD loadding];
    [self.webview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self).offset(0);
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
        make.height.mas_equalTo(300);
    }];


//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
//    [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
//        NSLog(@"走了 暂停方法");
//
//    }];
//    self.userInteractionEnabled = YES;
//    [self addGestureRecognizer:tap];
  
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(nonnull NSError *)error{
    
    NSLog(@"error1 : %@",error);
    NSLog(@"");
    
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismissVProgressHud:3];
//    [QMUITips showLoadingInView:self];
//
//        [QMUITips hideAllTipsInView:self];
    
    MainFirst_Magazine_VC_WX *vc = (MainFirst_Magazine_VC_WX *)[self getViewController:[MainFirst_Magazine_VC_WX class]];

    vc.Tbv.scrollEnabled = YES;

    //    //  高度自适应
    [RACObserve(self.webview.scrollView,contentSize)subscribeNext:^(id  _Nullable x) {
       
        CGSize fittingSize = [self.webview sizeThatFits:CGSizeZero];
        if (self.MaxH < fittingSize.height) {
            NSLog(@"内容高度变化了");
            self.MaxH = fittingSize.height;
            self.webview.frame = CGRectMake(0, 0,kScreenW-40, fittingSize.height);
            [self.webview mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(0);
                make.left.equalTo(self).offset(20);
                make.right.equalTo(self).offset(-20);
                make.bottom.equalTo(self).offset(0);
                
                make.height.mas_equalTo(fittingSize.height);
            }];
            MainFirst_Magazine_VC_WX *vc = (MainFirst_Magazine_VC_WX *)[self getViewController:[MainFirst_Magazine_VC_WX class]];
            [vc.Tbv reloadData];
        }
  
    }];
}




-(UIWebView *)webview{

    if (!_webview) {

        _webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-40, SCREEN_HEIGHT-100)];

        _webview.backgroundColor = [UIColor randomColor];

        _webview.opaque = NO;

        _webview.scrollView.pagingEnabled = NO;

        _webview.delegate = self;

        _webview.scrollView.bounces = NO;

        _webview.scrollView.showsHorizontalScrollIndicator = NO;

        _webview.scrollView.showsVerticalScrollIndicator = NO;

        }
    return _webview;
}

-(JSContext *)context{
    if (!_context) {
    
        _context = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

//        JSTestObjext *test = [JSTestObjext new];
        _context[@"JSObjectProtocol"] = self;
    }
    return _context;
}


-(void)getData{
    [Tools showTips:@"h5调用 getData方法成功"];
}


-(void)openPhoto:(NSString*)url{

//    self.imageV .tag = 100;
//    [self.imageV  showBigImage:100];
//    [Tools showTips:[NSString stringWithFormat:@"打开图片 ur : %@",url]];
    
//    BaseImageV *imageV = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
//    [self.imageV sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
//    self.PicSelectUrl = url;
//    [self showBigImage:self.imageV];
//    BaseImageV *imageV = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 100, 200) contentMode:2];
    //    [imageV sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
//    [self addSubview:imageV];
//    imageV.tag = 100;
//    [imageV showBigImage:100];
//    [imageV addBigImage];
    
//    BaseImageV *imageV = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
//    [imageV sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
//    [self addSubview:imageV];
//    [imageV showBigImage:100];
    
//    self.PicSelectUrl = url;
//    [self showBigImage:imageV];
//
    if (self.block) {
        _block(url);
    }
}


- (BaseImageV *)imageV{
    if (!_imageV) {
//        _imageV = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
//        [self addSubview:_imageV];
        _imageV = [BaseImageV initCustom:CGRectMake(0, 0, 100, 200) contentMode:2];
        
//        [_imageV addBigImage];
        [self addSubview:_imageV];
        
    }
    return _imageV;
}

-(void)openVideo:(NSString*)url{

    [Tools showTips:[NSString stringWithFormat:@"打开视频 ur : %@",url]];
    HomePageDetailModelpgcJson *model =[[HomePageDetailModelpgcJson alloc]init];
    model.src = url;
    MainFirst_MagazineVideo_VC_WX *vc = [[MainFirst_MagazineVideo_VC_WX alloc]init];
    vc.modelJson = model;
    [self.getNav pushViewController:vc animated:YES];
}


//-(void)showBigImage:(UIImageView*)imageView{
////    self.imageV = imageView;
//    UIImageView *imageV = self.imageV;
//    // self.model.fileList[selectIndex-100];
//
//    if (!self.imagePreviewViewController) {
//        self.imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init];
//        self.imagePreviewViewController.imagePreviewView.delegate = self;
//
//        // QMUIImagePreviewViewController 对于以 window 的方式展示的情况，默认会开启手势拖拽退出预览功能。
//        // 如果使用了手势拖拽，并且退出预览时需要飞到某个 rect，则需要实现这个 block，在里面自己去 exit，如果不实现这个 block，退出动画会使用 fadeOut 那种
//
//    }
//    @weakify(self);
//    self.imagePreviewViewController.customGestureExitBlock = ^(QMUIImagePreviewViewController *aImagePreviewViewController, QMUIZoomImageView *currentZoomImageView) {
//        @strongify(self);
//        UIImageView *imageV1 = imageView;
//        NSLog(@"33 %@",imageView);
//
//        CGRect rect = [imageV1 convertRect:imageV1.frame toView:nil];
//        /** 微调 */
//        rect.origin.x -= 10;
//        rect.origin.y -= 10;
//        [aImagePreviewViewController exitPreviewToRectInScreenCoordinate:rect];
//
//    };
//    self.imagePreviewViewController.imagePreviewView.currentImageIndex = 0;// 默认查看的图片的 index
//
//    [self.imagePreviewViewController startPreviewFromRectInScreenCoordinate:[imageV convertRect:imageV.frame toView:nil] cornerRadius:imageV.layer.cornerRadius];
//}


#pragma mark - <QMUIImagePreviewViewDelegate>


/**
 
 - (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView {
 return 1;
 }
 
 - (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index {
 // 模拟异步加载的情况
 zoomImageView.reusedIdentifier = @(index);
 [zoomImageView showLoading];
 
 if ([zoomImageView.reusedIdentifier isEqual:@(index)]) {
 //        UIImageView *imageV = ;
 
 
 [self.imageV sd_setImageWithURL:[NSURL URLWithString:self.PicSelectUrl] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
 [zoomImageView hideEmptyView];
 zoomImageView.image = self.imageV.image;
 NSLog(@"11 %@",zoomImageView.image);
 }];
 }
 }
 
 - (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index {
 return QMUIImagePreviewMediaTypeImage;
 }
 
 #pragma mark - <QMUIZoomImageViewDelegate>
 
 - (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location {
 //点击缩回
 NSLog(@"22 %@",zoomImageView);
 
 self.imagePreviewViewController.customGestureExitBlock(self.imagePreviewViewController, zoomImageView);
 }
 
 */


//-(void)showBigImage:(UIImageView*)imageView{
//    UIImageView *imageV = self.imageV;
//    // self.model.fileList[selectIndex-100];
//
//    if (!self.imagePreviewViewController) {
//        self.imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init];
//        self.imagePreviewViewController.imagePreviewView.delegate = self;
//
//        // QMUIImagePreviewViewController 对于以 window 的方式展示的情况，默认会开启手势拖拽退出预览功能。
//        // 如果使用了手势拖拽，并且退出预览时需要飞到某个 rect，则需要实现这个 block，在里面自己去 exit，如果不实现这个 block，退出动画会使用 fadeOut 那种
//
//    }
//    @weakify(self);
//    self.imagePreviewViewController.customGestureExitBlock = ^(QMUIImagePreviewViewController *aImagePreviewViewController, QMUIZoomImageView *currentZoomImageView) {
//        @strongify(self);
//        UIImageView *imageV1 = self.imageV;
//
//        [aImagePreviewViewController exitPreviewToRectInScreenCoordinate:[imageV1 convertRect:imageV1.frame toView:nil]];
//
//    };
//    self.imagePreviewViewController.imagePreviewView.currentImageIndex =  0;// 默认查看的图片的 index
//
//    [self.imagePreviewViewController startPreviewFromRectInScreenCoordinate:[imageV convertRect:imageV.frame toView:nil] cornerRadius:imageV.layer.cornerRadius];
//}


//- (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView {
//    return 1;
//}
//
//- (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index {
//    // 模拟异步加载的情况
//    zoomImageView.reusedIdentifier = @(index);
//    [zoomImageView showLoading];
//
//    if ([zoomImageView.reusedIdentifier isEqual:@(index)]) {
//        //        UIImageView *imageV = ;
//
//
//        [self.imageV sd_setImageWithURL:[NSURL URLWithString:self.PicSelectUrl] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//            [zoomImageView hideEmptyView];
//            zoomImageView.image = self.imageV.image;
//            NSLog(@"11 %@",zoomImageView.image);
//        }];
//    }
//}
//- (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index {
//    return QMUIImagePreviewMediaTypeImage;
//}
//
//#pragma mark - <QMUIZoomImageViewDelegate>
//
//- (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location {
//    //点击缩回
//    self.imagePreviewViewController.customGestureExitBlock(self.imagePreviewViewController, zoomImageView);
//}


//-(void)setWebData{
//    return;
//    if (self.isSetData) {
//        return;
//    }
//
//    if (self.dataStr.length == 0) {
//        return;
//    }
//
//    self.isSetData = YES;
//
//    NSString *str = self.dataStr;
//
//    str = [NSString stringWithFormat:@"setData('%@')",str];
//
//    str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
//
//    [self.context evaluateScript:str];
//
//}




-(void)showPauseAudio{
    NSLog(@"走了！ pauseAudio");
    [self.webview stringByEvaluatingJavaScriptFromString:@"pauseAudio()"];
}



-(void)showBigImage:(UIImageView*)imageView{
    self.imageV = imageView;
    UIImageView *imageV = imageView;
    // self.model.fileList[selectIndex-100];
    
    if (!self.imagePreviewViewController) {
        self.imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init];
        self.imagePreviewViewController.imagePreviewView.delegate = self;
        
        // QMUIImagePreviewViewController 对于以 window 的方式展示的情况，默认会开启手势拖拽退出预览功能。
        // 如果使用了手势拖拽，并且退出预览时需要飞到某个 rect，则需要实现这个 block，在里面自己去 exit，如果不实现这个 block，退出动画会使用 fadeOut 那种
        
    }
    @weakify(self);
    self.imagePreviewViewController.customGestureExitBlock = ^(QMUIImagePreviewViewController *aImagePreviewViewController, QMUIZoomImageView *currentZoomImageView) {
        @strongify(self);
        UIImageView *imageV1 = imageView;
        CGRect rect = [imageV1 convertRect:imageV1.frame toView:nil];
        /** 微调 */
        rect.origin.x -= 10;
        rect.origin.y -= 10;
        [aImagePreviewViewController exitPreviewToRectInScreenCoordinate:rect];
        
    };
    self.imagePreviewViewController.imagePreviewView.currentImageIndex = 0;// 默认查看的图片的 index
    
    [self.imagePreviewViewController startPreviewFromRectInScreenCoordinate:[imageV convertRect:imageV.frame toView:nil] cornerRadius:imageV.layer.cornerRadius];
}


#pragma mark - <QMUIImagePreviewViewDelegate>

- (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView {
    return 1;
}

- (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index {
    // 模拟异步加载的情况
    zoomImageView.reusedIdentifier = @(index);
    [zoomImageView showLoading];
    
    if ([zoomImageView.reusedIdentifier isEqual:@(index)]) {
        //        UIImageView *imageV = ;
        
        
        [self.imageV sd_setImageWithURL:[NSURL URLWithString:self.PicSelectUrl] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            [zoomImageView hideEmptyView];
            zoomImageView.image = self.imageV.image;
        }];
    }
}

- (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index {
    return QMUIImagePreviewMediaTypeImage;
}

#pragma mark - <QMUIZoomImageViewDelegate>

- (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location {
    //点击缩回
    self.imagePreviewViewController.customGestureExitBlock(self.imagePreviewViewController, zoomImageView);
}

@end
